<?php
/*
YHM (Your Hosting Manager) - Copyright 2010 All Rights Reserved. - YHM Group
Released under the Simplified BSD Licence.

Website: http://yhm.co.uk
Licence: http://yhm.co.uk/about/#licence

file: admin/packages.php
author: Kieran D. (Polarbear541)
*/

//Init Includes, Sessions and Page Info
require_once("../global.php");
session_start();
checklogin_admin();
$username = $_SESSION['user_name'];
$page = "settings";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YHM - Admin CP - Packages</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
</head>
<body>

<?php include("./header.php"); ?>

<div id="content">
	
	<?php
	//Assign Variables
	$action = $_GET['action'];
	$id = mysql_real_escape_string($_GET['id']);
	
	if ($action == "create") //If creating show form
	{
		echo "<h2>Creating Package</h2>";
	
		echo "<form action='./packages.php?action=do_create' method='POST'>
Public Name: <input type='text' name='name' /> WHM Name: <input type='text' name='whmname' /><br /><br />
Description: <br /><textarea name='desc' style='width:400px;height:150px;'></textarea><br /><br />
Active: <input type='checkbox' name='active' value='active' checked='yes' /><br /><br />
<input type='submit' value='Submit'></form><br />";
	}
	
	elseif ($action == "do_create") //If doing the creation run queries after standard checks
	{
		$name = trim(htmlspecialchars(mysql_real_escape_string($_POST['name'])));
		$whmname = trim(htmlspecialchars(mysql_real_escape_string($_POST['whmname'])));
		$desc = trim(htmlspecialchars(mysql_real_escape_string($_POST['desc'])));
		$active = trim(htmlspecialchars(mysql_real_escape_string($_POST['active'])));
		
		if ($active == "active")
		{
			$status = "1";
		}
		else
		{
			$status = "0";
		}
		
		if (empty($name) || empty($whmname) || empty($desc))
		{
			echo "<h2>There was a problem!</h2>";
			echo "Please fill in all the fields!<br />";
            echo "<a href='./packages.php?action=create'>Click here to go back</a><br />";
		}
		
		else
		{
			$createpkg = mysql_query("INSERT INTO ".TABLE_PREFIX."packages VALUES (NULL, '$name','$whmname','$desc','0','0','$status')");
			
			if (!$createpkg) //Error Handling
			{
				echo "<h2>There was a problem!</h2>";
				echo mysql_errno() . ": Query Failed" . mysql_error();
				echo "<a href='./packages.php?action=create'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Package Created Successfully</h2>";
				echo "<a href='./packages.php'>Click here to go back if not redirected</a><br />";
				redirect("./packages.php", 2);
			}
		}
	}
	
	elseif ($action == "edit") //If editing show form
	{
		//Run Queries to get id info
		$query = mysql_query("SELECT * FROM ".TABLE_PREFIX."packages WHERE id='$id'");
		$row = mysql_fetch_array($query);
		
		if ($id == null)
		{
		  die ("Please specify a package id to edit!<br /><br /><a href='./packages.php>Click here to go back</a><br />");
		  exit();
		}
		
		$check_package_query = "SELECT MAX(id) FROM ".TABLE_PREFIX."packages ORDER BY id DESC LIMIT 1";
		$check_package = mysql_query($check_package_query);
		$valid_package = mysql_result($check_package, 0);
       
	   if($id > $valid_package)
	   {
	     die ("The package you requested does not exist.<br /><br /><a href='./packages.php>Click here to go back</a><br />");
	     exit();
	   }
		
		if ($row['status'] == "1")
		{
			$checked = "checked='$active'";
		}
		
		if (!empty($id)) //Check if ID exists
		{
			echo "<h2>Editing Package - {$row['name']}</h2>";
	
			echo "<form action='./packages.php?action=do_edit&id=$id' method='POST'>
Public Name: <input type='text' name='name' value='{$row['name']}' /> WHM Name: <input type='text' name='whmname' value='{$row['whmname']}' /><br /><br />
Description: <br /><textarea name='desc' style='width:280px;height:200px;'>{$row['description']}</textarea><br /><br />
Active: <input type='checkbox' name='active' value='active' $checked /><br /><br />
<input type='submit' value='Submit'></form><br />";
		}
		
		else
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a package id to edit!<br /><a href='./packages.php'>Click here to go back</a>";
		}
	}
	
	elseif ($action == "do_edit") //If doing the edit run queries after standard checks
	{
		$name = trim(htmlspecialchars(mysql_real_escape_string(($_POST['name']))));
		$whmname = trim(htmlspecialchars(mysql_real_escape_string(($_POST['whmname']))));
		$desc = trim(htmlspecialchars(mysql_real_escape_string(($_POST['desc']))));
		$active = trim(htmlspecialchars(mysql_real_escape_string(($_POST['active']))));
	
		if ($active == "active")
		{
			$status = "1";
		}
		else
		{
			$status = "0";
		}
		
		if (empty($name) || empty($whmname) || empty($desc))
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please fill in all the fields!<br />";
            echo "<a href='./packages.php?action=edit&id=$id'>Click here to go back</a><br />";
		}
		
		elseif (!empty($id)) //Check if ID exists
		{
			$editpkg = mysql_query("UPDATE ".TABLE_PREFIX."packages SET name='$name', whmname='$whmname', description='$desc', type='0', group='0', status='$status' WHERE id=$id;");
			
			if (!$editpkg) //Error Handling
			{
				echo "<h2>There was a Problem!</h2>";
				echo "Error " . mysql_errno() . ": Query Failed - " . mysql_error() . "<br />";
				echo "<a href='./packages.php?action=edit&id=$id'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Package Edited Successfully</h2>";
				echo "<a href='./packages.php'>Click here to go back if not redirected</a><br />";
				redirect("./packages.php", 2);
			}
		}
		
		else
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a package id to edit!<br /><a href='./packages.php'>Click here to go back</a>";
		}
	}
	
	elseif ($action == "delete") //If Deleting Run Queries
	{
		if (empty($id)) //Check if ID exists
		{
			echo "<h2>There was a Problem!</h2>";
			echo "Please specify a package id to delete!<br /><a href='./packages.php'>Click here to go back</a>";
		}
		
		else
		{
			$delete = mysql_query("DELETE from ".TABLE_PREFIX."packages WHERE id='$id'");
			
			if (!$delete) //Error Handling
			{
				echo "<h2>There was a Problem!</h2>";
				echo "Error " . mysql_errno() . ": Query Failed" . mysql_error();
				echo "<a href='./packages.php'>Click here to go back</a><br />";
			}
			else
			{
				echo "<h2>Package Deleted Successfully</h2>";
				echo "<b>Please note that the package has not been deleted on the server side only in YHM!</b><br />";
				echo "<a href='./packages.php'>Click here to go back if not redirected</a><br />";
				redirect("./packages.php", 2);
			}
		}
	}
	
	else //Else list all
	{
		echo "<h2>Packages - <a href='./packages.php?action=create'>Create</a></h2>";
		echo "<table width='100%'>
<thead align='left'>
<th>Name</th>
<th>WHM Name</th>
<th>Description</th>
<th>Status</th>
<th>Options</th>
</thead>
<tbody>";
	
		$getpkgs = mysql_query("SELECT * FROM ".TABLE_PREFIX."packages ORDER by id DESC") or die("Error: Query Failed" . mysql_error());
	
		while ($pkglist = mysql_fetch_array($getpkgs))
		{
			//Convert Status ID's to Languages - 1:Active 0:Disabled
			if ($pkglist['status'] == "1")
			{
				$status = "Active";
			}
			elseif ($pkglist['status'] == "0")
			{
				$status = "Disabled";
			}
			
		
		
			echo "<tr>";
			echo "<td><a href='./packages.php?action=edit&id={$pkglist['id']}'>{$pkglist['name']}</a></td>";
			echo "<td>{$pkglist['whmname']}</td>";
			echo "<td>{$pkglist['description']}</td>";
			echo "<td>$status</td>";
			echo "<td><a href='./packages.php?action=edit&id={$pkglist['id']}'><img src='../images/edit.png' title='Edit' alt='Edit' /></a> <a href='./packages.php?action=delete&id={$pkglist['id']}'><img src='../images/delete.png' title='Delete' alt='Delete' /></a></td>";
			echo "</tr>";
		}
		
		echo "</tbody></table>";
	}
	?>
	
	<?php include '../footer.php'; ?>

</div>
</body>
</html>